<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

/**
 * 少儿报名商品管理
 * @author ASUS
 *
 */
class goods extends admin{
	private $db;
	public $siteid;
	public $admin_userid,$detail;
	
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('reg_child_goods_model');//加载少儿报名商品model
		$this->siteid = $this->get_siteid();// 获取当前站点ID
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
		$this->detail = pc_base::load_model('exchange_detail_model');
	}
	
	public function init() {
		//获取管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		
		$page = $_GET['page'] ? intval($_GET['page']) : 1;
		$where = "";
		if (isset($_GET['search'])) {
			$goods_name = $_GET['goods_name'];
			$where .= "name like '%$goods_name%' and ";
		}
		$where .= "siteid = ".$this->siteid." and status = 1";
		$goods = $this->db->listinfo($where,'id DESC',$page,10);
		$pages = $this->db->pages;
		include $this->admin_tpl('goods_list');
	}

	/**
	 * 商品兑换列表
	 */
	public function index() 
	{
		$page = $_GET['page']?(int)$_GET['page']:1;
        $num = $_GET['num']?(int)$_GET['num']:20;
        $sql = "select p.*,u.nickname from nsrweb_exchange_detail as p LEFT JOIN nsrweb_user_info as u on u.id = p.created_by";
		$where = '';
		if(isset($_GET['type']) && $_GET['type']) {
            $type = $_GET['type'];
            $where .= " where p.type = " . $type;
        }
        if(isset($_GET['source_id']) && $_GET['source_id']) {
            $source_id = $_GET['source_id'];
            $where .= $where?" AND p.source_id = " . $source_id:" where p.source_id = " . $source_id;
        }
		$sql .= $where . " order by p.id desc ";
        $lists = $this->detail->my_listinfos($sql, $page, $num);
        $pages = $this->detail->pages;
        //商品列表
        $goods = $this->db->goodsList();
        //获取管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
        //var_dump($lists);die;
        include $this->admin_tpl('detail_index');
	}

	/**
	 * 商品兑换状态修改
	 */
	public function goods_status_edit ()
	{
		$result = $this->detail->update(array('status'=> $_GET['status'],'update_time'=>date('Y-m-d H:i:s'),'updated_by'=>param::get_cookie('userid')), array(
            'id' => $_GET['id']
        ));
        
        if($result){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"更新成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"更新失败"));
        }
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$goods = isset($_POST['goods']) ? $_POST['goods'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$goods['create_time'] = $goods['update_time'] = date('Y-m-d H:i:s',time());
			$goods['created_by'] = $goods['updated_by'] = $this->admin_userid;
			$result = $this->db->insert($goods,true);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:#00CC00;">添加商品</span>[商品id：'.$result.'，商品名称：'.$goods['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('operation_success'),'?m=reg_child&c=goods&a=init');
			} else {
				showmessage(L('operation_failure'),HTTP_REFERER);
			}
		} else {
			include $this->admin_tpl('goods_add');
		}
	}
	
	public function edit() {
		if (!isset($_GET['id'])) {
			showmessage(L('illegal_parameters'),HTTP_REFERER);
		}
		$id = $_GET['id'];
		if (isset($_POST['dosubmit'])) {
			$goods =$_POST['goods'];
			$goods['update_time'] = date('Y-m-d H:i:s');
			$goods['updated_by'] = $this->admin_userid;
			$result = $this->db->update($goods,array('id'=>$id));
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改商品</span>[商品id：'.$result.'，商品名称：'.$goods['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('operation_success'),'?m=reg_child&c=goods&a=init');
			} else {
				showmessage(L('operation_failure'),HTTP_REFERER);
			}
		} else {
			$info = $this->db->get_one(array('id'=>$id));
			include $this->admin_tpl('goods_edit');
		}
	}
	
	public function del() {
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$del_id = str_replace("'","",$id);
		$del_name = $this->db->select("id in ('$id')","name","","id DESC");
		foreach ($del_name as $v){
			$name[] = $v['name'];
		}
		$result = $this->db->update(array(
				'status' => '2'
		), "id in ('$id')");
		if ($result) {
			//记录行为日志
			$log_content = '<span style="color:red;">删除商品</span>[商品id：'.$del_id.',商品名称：'.json_encode($name,JSON_UNESCAPED_UNICODE).']';
			$this->manage_admin_log($log_content);
			showmessage(L('operation_success'), '?m=reg_child&c=goods&a=init');
		} else {
			showmessage(L('operation_failure'), HTTP_REFERER);
		}
	}
}